package com.zhengke.controller.ddgl;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhengke.pojo.AjaxResult;
import com.zhengke.pojo.NewMemberREntity;
import com.zhengke.pojo.SysOrderResult;
import com.zhengke.pojo.UserTest;
import com.zhengke.service.MemberManagerService;

@Controller
@RequestMapping("/memberCon")
public class MemberManagerController {
	
	@Autowired
	MemberManagerService memberMangerService;
	
	@RequestMapping("/goTomemberJsp.do")
	public String goToManagerJsp() {
		return "/memberManager/memberManager";
	}
	
	/**
	   * 打开添加会员的方法
	 */
	@RequestMapping("/toAddUser.do")
	public String openAddUserJsp() {
		return "/memberManager/addUser";
	}
	
	@RequestMapping("/toShowTable.do")
	@ResponseBody
	public SysOrderResult getAllMemberManager(String userStatu,String userName,String userPhone,String userNumberCard,String userId,int page,int limit) {
		PageHelper.startPage(page, limit, true);
		System.out.println("userStatu=="+userStatu+"userName=="+userName+"userPhone=="+userPhone+"userNumberCard=="+userNumberCard+"userId"+userId);
		//获取数据进行查询23条数据
		Map<String,Object> messageMap = new HashMap<String, Object>();
		messageMap.put("user_name", userName);
		messageMap.put("user_phone", userPhone);
		messageMap.put("statu", userStatu);
		messageMap.put("user_card_number", userNumberCard);
		messageMap.put("user_id", userId);
		List<NewMemberREntity> memberEntityList = memberMangerService.querrAllMemberMethod(messageMap);
		PageInfo<NewMemberREntity> pageInfo = new PageInfo<NewMemberREntity>(memberEntityList);
		//创建返回值对象
		SysOrderResult sysOrderResult = null;
		try {
			sysOrderResult = new SysOrderResult();
			sysOrderResult.setCode("0");
			System.err.println(pageInfo.getTotal());
			sysOrderResult.setData(memberEntityList);
			sysOrderResult.setCount(23L);
		}catch(Exception e) {
			e.getStackTrace();
		}
		return sysOrderResult;
	}
	
	/**
	   * 删除会员
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteUser.do")
	@ResponseBody
	public AjaxResult deleteUserById(String id) {
		int result ;
		try {
			result = memberMangerService.deleteUserByID(id);
			if(result > 0) {
				return new AjaxResult("200","删除成功！");
			}else {
				return new AjaxResult("300","*删除失败*");
			}
		}catch(Exception e) {
			return new AjaxResult("300","系统异常！");
		}
	}
	
	/**
	 * 禁用会员
	 */
	@RequestMapping("/disableUserById.do")
	@ResponseBody
	public AjaxResult disableUserById(String id) {
		int result ;
		//首先查询当前的状态
		String userStatuString =memberMangerService.querryStatuById(id);
		System.err.println("============================>"+userStatuString);
		if("0".equals(userStatuString)) {
			return new AjaxResult("300","··已经被禁用了还禁用个毛啊··");
		}else {
			result = memberMangerService.disableUserById(id);
			if(result > 0) {
				return new AjaxResult("200","禁用成功！");
			}else {
				return new AjaxResult("300","禁用失败！");
			}
		}
	}
	
	/**
	 * 激活会员
	 */
	@RequestMapping("/activationUserById.do")
	@ResponseBody
	public AjaxResult activationUserById(String id) {
		int result;
		//查询状态
		String userStatuString =memberMangerService.querryStatuById(id);
		System.err.println("============================>"+userStatuString);
		if("1".equals(userStatuString)) {
			return new AjaxResult("300","··已经激活了不能再激活了··");
		}else {
			result = memberMangerService.activationUserById(id);
			if(result > 0) {
				return new AjaxResult("200","激活成功！");
			}else {
				return new AjaxResult("300","激活失败！");
			}
		}
	}
	
	/**
	   * 添加会员的方法
	 */
	@RequestMapping("/addUsermethod.do")
	@ResponseBody
	public AjaxResult addUserMethod(HttpServletRequest request,HttpServletResponse response) {
		String user_id = request.getParameter("user_id");
		String user_name = request.getParameter("user_name");
		String user_phone = request.getParameter("user_phone");
		String address_detail = request.getParameter("address_detail");
		String user_integral =  request.getParameter("user_integral");
		double integer = Double.parseDouble(user_integral);
		DecimalFormat df = new DecimalFormat( "0.00");
		String canNumber = df.format(integer);
		double newIngeger = Double.parseDouble(canNumber);
		UserTest usetResTest = new UserTest();
		usetResTest.setUser_id(user_id);
		usetResTest.setUser_name(user_name);
		usetResTest.setUser_phone(user_phone);
		usetResTest.setAddress_detail(address_detail);
		usetResTest.setUser_integral(newIngeger);
		int result = memberMangerService.inSerectUserMethod(usetResTest);
		if(result > 0) {
			return new AjaxResult("200","··添加成功··");
		}else {
			return new AjaxResult("300","添加失败！！");
		}
	}
}
